﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    //
    public class SDTFWBData : BaseFunction
    {

        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public SDTFWBData()
            : base()
        {
             
        }
        public SDTFWBData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        { 
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public SDTFWBData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            
        } 
        
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                Result = INTEXDBC.QueryABKOPF(dwInput);
                Return.Merge(Result);
                DataRow rowtemp = null;
                foreach (DataRow row in Return.Tables["ABKOPF"].Rows)
                {
                    rowtemp = INTEXDB.ABPOS.NewRow();
                    rowtemp["ABPMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ABPAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ABPAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPOS(rowtemp, 2);
                    Return.Merge(Result);
                }

                foreach (DataRow row in Return.Tables["ABPOS"].Rows)
                {
                    rowtemp = INTEXDB.ABPR.NewRow();
                    rowtemp["ARMANDANT"] = row["ABPMANDANT"];
                    rowtemp["ARAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["ARAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["ARAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABPR(rowtemp);
                    Return.Merge(Result);
                    
                    rowtemp = INTEXDB.WASTAMM.NewRow();
                    rowtemp["WAMANDANT"] = row["ABPMANDANT"];
                    rowtemp["WAART"] = row["ABPART"];
                    rowtemp["WAVAR"] =Left( row["ABPVAR"].ToString(),2);
                    Result = INTEXDBC.QueryWASTAMM(rowtemp);
                    Return.Merge(Result);

                    rowtemp = INTEXDB.WAFARBEN.NewRow();
                    rowtemp["WAFMANDANT"] = row["ABPMANDANT"];
                    rowtemp["WAFART"] = row["ABPART"];
                    rowtemp["WAFVAR"] =Left(row["ABPVAR"].ToString(), 2);
                    Result = INTEXDBC.QueryWAFARBEN(rowtemp,2);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["ABPMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["ABAAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABAUFM(rowtemp,2);
                    Return.Merge(Result);
                                        
                    rowtemp = INTEXDB.ABFOLGE.NewRow();
                    rowtemp["ABFMANDANT"] = row["ABPMANDANT"];
                    rowtemp["ABFAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["ABFAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["ABFAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABFOLGE(rowtemp,2);
                    Return.Merge(Result);
                    string processed = "";
                    foreach(DataRow rowf in Result.Tables[0].Rows)
                    {
                        string ABFLIEFNR = rowf["ABFLIEFNR"].ToString().Trim();
                        
                        if (ABFLIEFNR == "") continue;
                        
                        if (processed.Contains(ABFLIEFNR + ";")) continue;
                        
                        processed += ABFLIEFNR+";";

                        rowtemp = INTEXDB.LIEFDAT.NewRow();
                        rowtemp["LSMANDANT"] = rowf["ABFMANDANT"];
                        rowtemp["LSLIEFNR"] = rowf["ABFLIEFNR"];                        
                        Result = INTEXDBC.QueryLIEFDAT(rowtemp, 2);
                        Return.Merge(Result);

                    }
                    string order= "";
                    string remark = "";
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + "   ";
                    remark += GetCommentsFromTXTIND("AB3", order).Trim();
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                    remark += GetCommentsFromTXTIND("ABP", order).Trim();
                    row["ABPFILLER1"] = remark;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

    }
}